﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;

namespace auxiliary_proj
{
    class Parser
    {
        //Must be between 3-15 characters | a miz=xture of char/number/symbols
        public bool testUserNameRegex(string username)
        {
            string reg = @"^(?=.{3,15}$)([A-Za-z0-9][._()\[\]-]?)*$";
            Regex r = new Regex(reg);
            bool isMatch = r.IsMatch(username); ;
            return isMatch;
        }
        //be at least 8 characters, and to be a mix of letters and numbers.
        public bool testUserPasswordRegex(string password)
        {
            string reg=@"(?=.{8,})[a-zA-Z]+[^a-zA-Z]+|[^a-zA-Z]+[a-zA-Z]+";
            Regex r=new Regex(reg);
            
            bool isMatch=r.IsMatch(password);
            return isMatch;
        }
        //Regex to validate email address
        public bool testEmailRegex(string email)
        {
            string reg = @"\w+@\w+\.+\w{2,}";
            Regex r = new Regex(reg);
            bool isMatch = r.IsMatch(email);
            return isMatch;
        }
        //Regex to validate account balance
        public bool testAccountBalRegex(string balance)
        {
            string reg = @"\d+";
            Regex r = new Regex(reg);
            bool isMatch = r.IsMatch(balance);
            return isMatch;
        }

        /// <summary>
        /// Handles TextBox input pattern matching
        /// while also coloring the TextBox according to valid/invalid/empty match
        /// </summary>
        /// <param name="sender">TextBox object from the TextChanged event</param>
        /// <param name="pattern">pattern to match</param>
        /// <returns></returns>
        public static bool isMatch(object sender, string pattern)
        {
            bool valid = false;

            if (((TextBox)sender).Text.Equals(""))
                ((TextBox)sender).BackColor = SystemColors.Info;
            else
            {
                //run regex when text changes. if invalid, make textbox red, if valid make green
                //this pattern is any alpha-numeric value with length between 4 and 10
                valid = Regex.Match(((TextBox)sender).Text, pattern).Success ? true : false;

                if (valid)
                    //make a greenish color
                    ((TextBox)sender).BackColor = System.Drawing.ColorTranslator.FromHtml("#B9FF94");
                else
                    //make a redish color
                    ((TextBox)sender).BackColor = System.Drawing.ColorTranslator.FromHtml("#FF7D5D");
            }

            return valid;
        }
    }
}
